package com.dq.repository.order;

import com.dq.domain.goods.Product;
import com.dq.domain.orders.Orders;
import com.dq.domain.orders.UserOrder;
import com.dq.domain.orders.info.OrderInfo;
import com.dq.domain.org.Org;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liuhui
 * @version 1.0
 * @date 2019/9/23 0023 下午 23:46
 */
@Repository
public interface OrderInfoRepository extends JpaRepository<OrderInfo,Long> {
    List<OrderInfo> findByOrders(Orders orders);

    List<OrderInfo> findByProductAndCreateTimeLessThanEqualAndCreateTimeGreaterThanEqualAndDeletedFalse(Product product,Long beginTime,Long endTime);
    @Query(nativeQuery = true, value = "SELECT COALESCE(sum(oi.amount),0) FROM order_info oi LEFT JOIN orders o \n" +
            "ON oi.orders_id = o.id\n" +
            "where o.`status`=?1 and o.org_id=?2 AND o.create_time <= ?3 AND o.create_time >= ?4 and oi.product_id=?5")
    BigDecimal countSumPrice(UserOrder.UserOrderStatus status, Long orgId,Long beginTime,Long endTime,Long productId);

    @Query(nativeQuery = true, value = "SELECT COALESCE(sum(oi.number),0) FROM order_info oi LEFT JOIN orders o \n" +
            "ON oi.orders_id = o.id\n" +
            "where o.`status`=?1 and o.org_id=?2 AND o.create_time <= ?3 AND o.create_time >= ?4 and oi.product_id=?5")
    Long countSumNum(UserOrder.UserOrderStatus status, Long orgId,Long beginTime,Long endTime,Long productId);

    OrderInfo findByOrdersAndDeletedFalse(Orders orders);
}
